package com.feeyo.groundservice.vehiclemonitorserver;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diabolicallabs.vertx.cron.CronObservable;
import com.feeyo.groundservice.base.influx.Influx;
import com.feeyo.groundservice.base.rabbitmq.RMQ;
import com.feeyo.groundservice.base.rdb.PostgresDb;
import com.feeyo.groundservice.base.redis.Redis;
import com.feeyo.groundservice.base.util.PropertiesUtils;

import io.vertx.core.DeploymentOptions;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.rx.java.RxHelper;
import rx.Scheduler;

/**
 * Created by fooevr on 2016/12/22.
 */
public class Bootstrap {
	static Logger logger = LoggerFactory.getLogger(Bootstrap.class);
	
	
    public static void main(String[] args){
    	System.setProperty("user.timezone","GMT+08"); 
    	Date date = new Date();
    	logger.info("current-time:"+date.toString());
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	logger.info("date.format:"+sdf.format(date));
    	
        Vertx vertx = Vertx.vertx();
        DeploymentOptions opt = new DeploymentOptions();
        opt.setWorker(true);
        //opt.setMultiThreaded(multiThreaded)
        vertx.deployVerticle(new Influx(),opt);	  //注册influxDb服务
        vertx.deployVerticle(new Redis(),opt,r ->{//注册redis服务
            vertx.deployVerticle(new PostgresDb(), u->{  //注册postgreSql服务
                if (u.failed()){
                    System.out.println(u.cause());
                    return;
                }
                CountDownLatch cdl = new CountDownLatch(5);
                //注册MQ服务，并订阅MQ（在基础数据加载后执行）
                Handler<?> loadBaseComp = event -> {
                    if(cdl.getCount() == 0) {
                        
                    }
                };
                /**
                 * 定期（每分钟）从数据库拉取基础数据到本地内存
                 */
                vertx.deployVerticle(new LimitAreaManager(),opt, x->{
                    cdl.countDown();
                    loadBaseComp.handle(null);
                });
                vertx.deployVerticle(new Department(),opt, x->{
                    cdl.countDown();
                    loadBaseComp.handle(null);
                }); 
                vertx.deployVerticle(new VehicleType(), opt,x->{
                    cdl.countDown();
                    loadBaseComp.handle(null);
                });
                vertx.deployVerticle(new VehicleInfo(),opt, x->{
                    cdl.countDown();
                    loadBaseComp.handle(null);
                });
                vertx.deployVerticle(new CrossingManager(),opt, x->{
                    cdl.countDown();
                    loadBaseComp.handle(null);
                });
                
                Scheduler scheduler = RxHelper.scheduler(vertx);
                //scheduler.createWorker().schedule(action, delayTime, unit)
                CronObservable.cronspec(scheduler, "0 0/1 * * * ? *", "Asia/Shanghai")
                        //.take(5)
                        .subscribe(
                                timestamped -> {
                                    // sync base data
                                    vertx.deployVerticle(new LimitAreaManager(),x->{});
                                    vertx.deployVerticle(new Department(),x->{});
                                    vertx.deployVerticle(new VehicleType(),x->{});
                                    vertx.deployVerticle(new VehicleInfo(),x->{});
                                    vertx.deployVerticle(new CrossingManager(),x->{});
                                },
                                fault -> {
                                }
                );
                CronObservable.cronspec(scheduler, "0/1 * * * * ? *", "Asia/Shanghai")
                .subscribe(
                        timestamped -> {
                            // sync base data
                            vertx.deployVerticle(new AlarmService(),x->{});
                        },
                        fault -> {
                        }
                );
                
            });
          //计数周期开始
            AlarmService.startTime = System.currentTimeMillis();
            vertx.deployVerticle(new MonitorService(), opt);
        });
        
    }



}
